package com.sunday.aomicaichang.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.base.BaseApp;
import com.sunday.aomicaichang.model.Village;
import com.sunday.aomicaichang.ui.SelectAreaActivity;
import com.sunday.member.entity.addr.Address;
import com.sunday.member.http.MemberClient;
import com.sunday.member.utils.SignUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class AddAddressActivity extends BaseActivity {

    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tv_name)
    ClearEditText tvName;
    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.tv_phone)
    ClearEditText tvPhone;
    @Bind(R.id.tv_place)
    TextView tvPlace;
    @Bind(R.id.btn_save)
    TextView btnSave;
    @Bind(R.id.tv_finalAddress)
    ClearEditText tvFinalAddress;

    private Address address;
    private long memberId;
    private int addressId;
    private int villageId=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_addr);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        memberId = BaseApp.getInstance().getMember().getId();
        address = (Address) getIntent().getSerializableExtra("address");
        if (address == null) {
            title.setText("添加地址");
        } else {
            title.setText("编辑地址");
            if(address.getReceiveName()!=null)
            tvName.setText(address.getReceiveName());
            if(address.getVillageName()!=null)
            tvPlace.setText(address.getVillageName());
            if(address.getReceiveMobile()!=null)
            tvPhone.setText(address.getReceiveMobile());
            if(address.getReceiveAddress()!=null)
            tvFinalAddress.setText(address.getReceiveAddress());
            addressId = address.getId();
            villageId=address.getVillageId();
        }
    }


    @OnClick({R.id.btn_save,R.id.tv_place})
    void onClick(View v){
        switch (v.getId()) {
            case R.id.btn_save:
                saveAddr();
                break;
            case R.id.tv_place:
                intent = new Intent(mContext, SelectAreaActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode!=RESULT_OK){
            return;
        }
        Village.ChildrenBeanX.ChildrenBean childrenBean = (Village.ChildrenBeanX.ChildrenBean) data.getSerializableExtra("area");
        if (childrenBean.getName() != null)
            tvPlace.setText(childrenBean.getName());
        villageId=childrenBean.getId();

    }

    private void saveAddr(){
        String userName=tvName.getText().toString().trim();
        String mobileNo=tvPhone.getText().toString().trim();
        String addrDetail=tvFinalAddress.getText().toString().trim();
        String addr=tvPlace.getText().toString().trim();
        if (TextUtils.isEmpty(userName)){
            ToastUtils.showToast(mContext,"请输入收货人姓名");
            return;
        }
        if (!StringUtils.isMobileNO(mobileNo)){
            ToastUtils.showToast(mContext,"请输入正确手机号");
            return;
        }
        if(villageId==0){
            ToastUtils.showToast(mContext,"请填写地址");
            return;
        }
        if (TextUtils.isEmpty(addrDetail)||TextUtils.isEmpty(addr)){
            ToastUtils.showToast(mContext,"请填写具体收货地址");
            return;
        }
        showLoadingDialog(0);
        String nonce= MD5Utils.random();
        String[] str1=new String[]{"id","villageId","receiveName","receiveMobile","receiveAddress","nonce"};
        String[] str2=new String[]{String.valueOf(addressId),String.valueOf(villageId),userName,mobileNo,addrDetail,nonce};

        Call<ResultDO> call= MemberClient.getApiAdapter().saveAddr(addressId,villageId,userName,mobileNo,null,null,null,null,null,null,addrDetail,nonce,SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish){return;}
                dissMissDialog();
                if (response.body()==null){return;}
                if (response.body().getCode()==0){
                    ToastUtils.showToast(mContext,"保存成功");
                    finish();
                }else{
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext,R.string.network_error);
            }
        });
    }
}
